//
//  QSWorkStationMyWorkViewController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/5/20.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSWorkStationMyWorkViewController.h"
#import "QSWorkStationHeaderView.h"
#import "QSWorkStateWithButtonCell.h"
#import "QSWorkStationInterface.h"
#import "QSWorkStationFileModel.h"
@interface QSWorkStationMyWorkViewController ()
@property (nonatomic, strong)QSWorkStationHeaderView *headerView;
@property (nonatomic, strong)QSWorkDetailsModel *detailModel;
@property (nonatomic, assign)CGFloat headerHeight;
@property (nonatomic, assign)CGFloat headerViewTableHeight;

@end
static NSString *buttonCell = @"WorkStateWithButtonCell";
static CGFloat photoCellHeight = 64;
static CGFloat officeCellHeight = 50;
@implementation QSWorkStationMyWorkViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"工作台";
    [self configSubView];
    [self getWorkDetailsWithId:self.workId];
//    self.tableView.tableHeaderView = self.headerView;
}
- (void)configSubView{
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    [self.tableView registerNib:[UINib nibWithNibName:@"QSWorkStateWithButtonCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:buttonCell];
}
- (QSWorkStationHeaderView *)headerView{
    if (!_headerView) {
        self.headerHeight = kRealValue(kScreenHeight * 2);
        _headerView = [[QSWorkStationHeaderView alloc]initWithFrame:CGRectMake(10, 10, kScreenWidth - 20, _headerHeight)];
        _headerView.backgroundColor = [UIColor clearColor];
        _headerView.layer.cornerRadius = 5;
        WeakSelf(weakSelf);
        _headerView.didOpenFileListBlock = ^(BOOL isOpen) {
            weakSelf.headerView.closeBackGroudView.hidden = isOpen;
            weakSelf.headerView.tableView.hidden = !isOpen;
            if (isOpen) {
                NSLog(@"展开");
                weakSelf.headerView.height = weakSelf.headerView.closeBackGroudView.maxY + weakSelf.headerViewTableHeight;
                weakSelf.tableView.tableHeaderView = weakSelf.headerView;
            }else{
                NSLog(@"关闭");
                weakSelf.headerView.height = weakSelf.headerView.closeBackGroudView.maxY + weakSelf.headerView.closeBackGroudView.height ;
                weakSelf.tableView.tableHeaderView = weakSelf.headerView;
            }
        };
    }
    return _headerView;
}
#pragma mark - api
//获取头部展示数据
- (void)getWorkDetailsWithId:(NSString *)workId{
    [QSAppKeyWindow showIndeterminateHudWithText:@""];
    [QSWorkStationInterface getMyWorkDetailsWithId:workId andBlock:^(ResponseMessage *rspStatusAndMessage, QSWorkDetailsModel *resultModel) {
        [QSAppKeyWindow hideHud];
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            self.detailModel = resultModel;
            [self updateHeaderView];//赋值头部
            [self updateTabelViewHeight];//计算tableview高度
//            [self tableViewShouldUpdateDataByPageIndex:1];
        }
    }];
}

- (void)tableViewShouldUpdateDataByPageIndex:(NSInteger)pageIndex{
    
}
//更新头部展示数据
- (void)updateHeaderView{
    self.headerView.model = self.detailModel;
}

//计算高度
- (void)updateTabelViewHeight{
    
    if (self.detailModel.workFileList) {
        for (QSWorkStationFileModel *fileModel in _detailModel.workFileList) {
            if (fileModel.ewFileChoose) {//存在则计算
                if ([fileModel.ewFileType isEqualToString:@"photo"] || [fileModel.ewFileType isEqualToString:@"other"]) {
                    self.headerViewTableHeight += kRealValue(photoCellHeight);
                }else{
                    self.headerViewTableHeight += kRealValue(officeCellHeight);
                }
            }
        }
    }else{
    }
    NSLog(@"计算cell总高度为：%.2f", self.headerViewTableHeight);
    self.tableView.tableHeaderView = self.headerView;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //默认的高度
        self.headerView.tableView.height = self.headerViewTableHeight;
        NSLog(@"默认总高度为：%.2f", self.headerView.tableView.height);
       self.headerView.height = self.headerView.closeBackGroudView.maxY + self.headerView.closeBackGroudView.height;
        self.tableView.tableHeaderView = self.headerView;
        if (self.detailModel.workFileList.count) {
            self.headerView.openBackgroudView.userInteractionEnabled = YES;
            self.headerView.closeBackGroudView.userInteractionEnabled = YES;

        }else{
            self.headerView.openBackgroudView.userInteractionEnabled = NO;
            self.headerView.closeBackGroudView.userInteractionEnabled = NO;

        }
    });
}
#pragma mark -- TableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QSWorkStateWithButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:buttonCell];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.detailModel;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kRealValue(250);
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    [self.tableView endEditing:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
